﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GoodMan.Infrastructure.Entities.Entity;
using GoodMan.Infrastructure.Entities;
using GoodMan.Infrastructure.Entities.Repository;

namespace GoodMan.Portal.Biz
{
    public class ProductRepertoryBiz
    {
        public static List<ProductRepertory> GetProductRepertoryByProductId(int productid)
        {
            using (Context ctx = new Context())
            {
                var data = (from c in ctx.Dormitory
                            join di in ctx.ProductRepertory
                             on c.Id equals di.DormitoryId into dt
                            from d in dt.DefaultIfEmpty()
                            where d.ProductId == productid
                            select new {d,c})
                           .ToList()
                           .Select(e => new ProductRepertory().Set(e.d)
                               .LoadDormitory(e.c))
                           .ToList();
                var list = data;
                return list;
            }

        }
        public static bool SaveproductRepertory(List<ProductRepertory> productRepertoryList, int productId)
        {
            foreach (var productRepertory in productRepertoryList)
            {
                productRepertory.ProductId = productId;
                productRepertory.PostAdd();
            }
            return true;
        }

        public static bool SaveproductRepertoryHistory(List<ProductRepertoryHistory> productRepertoryHistoryList, int productId)
        {
            foreach (var productRepertoryHistory in productRepertoryHistoryList)
            {
                productRepertoryHistory.ProductId = productId;
                productRepertoryHistory.PostAdd();
            }
            return true;
        }
        public static bool DelProductRepertory(int productID)
        {
            try
            {
                
                using (Context ctx = new Context())
                {
                    ctx.Delete<T_ProductRepertory>()
                        .WhereSet(c => c.ProductId, WhereOperator.Equal, productID)
                        .End()
                        .Excute();
                }
                using (Context ctx = new Context())
                {
                    ctx.Delete<T_ProductRepertoryHistory>()
                        .WhereSet(c => c.ProductId, WhereOperator.Equal, productID)
                        .End()
                        .Excute();
                }
               
                return true;
            }
            catch (Exception ex)
            {
                //日志记录
                return false;
            }
            return false;
        }
    }
}
